<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>performancetimer.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_testing_performancetimer.js.html">performancetimer.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2008 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Performance timer.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>goog.provide(&#39;goog.testing.PerformanceTimer&#39;);
<a name="line20"></a>
<a name="line21"></a>goog.require(&#39;goog.array&#39;);
<a name="line22"></a>goog.require(&#39;goog.math&#39;);
<a name="line23"></a>
<a name="line24"></a>
<a name="line25"></a>
<a name="line26"></a>/**
<a name="line27"></a> * Creates a performance timer that runs test functions a number of times to
<a name="line28"></a> * generate timing samples, and provides performance statistics (minimum,
<a name="line29"></a> * maximum, average, and standard deviation).
<a name="line30"></a> * @param {number} opt_numSamples Number of times to run the test function;
<a name="line31"></a> *     defaults to 10.
<a name="line32"></a> * @param {number} opt_timeoutInterval Number of milliseconds after which the
<a name="line33"></a> *     test is to be aborted; defaults to 5 seconds (5,000ms).
<a name="line34"></a> * @constructor
<a name="line35"></a> */
<a name="line36"></a>goog.testing.PerformanceTimer = function(opt_numSamples, opt_timeoutInterval) {
<a name="line37"></a>  /**
<a name="line38"></a>   * Number of times the test function is to be run; defaults to 10.
<a name="line39"></a>   * @type {number}
<a name="line40"></a>   * @private
<a name="line41"></a>   */
<a name="line42"></a>  this.numSamples_ = opt_numSamples || 10;
<a name="line43"></a>
<a name="line44"></a>  /**
<a name="line45"></a>   * Number of milliseconds after which the test is to be aborted; defaults to
<a name="line46"></a>   * 5,000ms.
<a name="line47"></a>   * @type {number}
<a name="line48"></a>   * @private
<a name="line49"></a>   */
<a name="line50"></a>  this.timeoutInterval_ = opt_timeoutInterval || 5000;
<a name="line51"></a>
<a name="line52"></a>  /**
<a name="line53"></a>   * Whether to discard outliers (i.e. the smallest and the largest values)
<a name="line54"></a>   * from the sample set before computing statistics.  Defaults to false.
<a name="line55"></a>   * @type {boolean}
<a name="line56"></a>   * @private
<a name="line57"></a>   */
<a name="line58"></a>  this.discardOutliers_ = false;
<a name="line59"></a>};
<a name="line60"></a>
<a name="line61"></a>
<a name="line62"></a>/**
<a name="line63"></a> * @return {number} The number of times the test function will be run.
<a name="line64"></a> */
<a name="line65"></a>goog.testing.PerformanceTimer.prototype.getNumSamples = function() {
<a name="line66"></a>  return this.numSamples_;
<a name="line67"></a>};
<a name="line68"></a>
<a name="line69"></a>
<a name="line70"></a>/**
<a name="line71"></a> * Sets the number of times the test function will be run.
<a name="line72"></a> * @param {number} numSamples Number of times to run the test function.
<a name="line73"></a> */
<a name="line74"></a>goog.testing.PerformanceTimer.prototype.setNumSamples = function(numSamples) {
<a name="line75"></a>  this.numSamples_ = numSamples;
<a name="line76"></a>};
<a name="line77"></a>
<a name="line78"></a>
<a name="line79"></a>/**
<a name="line80"></a> * @return {number} The number of milliseconds after which the test times out.
<a name="line81"></a> */
<a name="line82"></a>goog.testing.PerformanceTimer.prototype.getTimeoutInterval = function() {
<a name="line83"></a>  return this.timeoutInterval_;
<a name="line84"></a>};
<a name="line85"></a>
<a name="line86"></a>
<a name="line87"></a>/**
<a name="line88"></a> * Sets the number of milliseconds after which the test times out.
<a name="line89"></a> * @param {number} timeoutInterval Timeout interval in ms.
<a name="line90"></a> */
<a name="line91"></a>goog.testing.PerformanceTimer.prototype.setTimeoutInterval = function(
<a name="line92"></a>    timeoutInterval) {
<a name="line93"></a>  this.timeoutInterval_ = timeoutInterval;
<a name="line94"></a>};
<a name="line95"></a>
<a name="line96"></a>
<a name="line97"></a>/**
<a name="line98"></a> * Sets whether to ignore the smallest and the largest values when computing
<a name="line99"></a> * stats.
<a name="line100"></a> * @param {boolean} discard Whether to discard outlier values.
<a name="line101"></a> */
<a name="line102"></a>goog.testing.PerformanceTimer.prototype.setDiscardOutliers = function(discard) {
<a name="line103"></a>  this.discardOutliers_ = discard;
<a name="line104"></a>};
<a name="line105"></a>
<a name="line106"></a>
<a name="line107"></a>/**
<a name="line108"></a> * @return {boolean} Whether outlier values are discarded prior to computing
<a name="line109"></a> *     stats.
<a name="line110"></a> */
<a name="line111"></a>goog.testing.PerformanceTimer.prototype.isDiscardOutliers = function() {
<a name="line112"></a>  return this.discardOutliers_;
<a name="line113"></a>};
<a name="line114"></a>
<a name="line115"></a>
<a name="line116"></a>/**
<a name="line117"></a> * Executes the test function the required number of times (or until the
<a name="line118"></a> * test run exceeds the timeout interval, whichever comes first).  Returns
<a name="line119"></a> * an object containing the following:
<a name="line120"></a> * &lt;pre&gt;
<a name="line121"></a> *   {
<a name="line122"></a> *     &#39;average&#39;: average execution time (ms)
<a name="line123"></a> *     &#39;count&#39;: number of executions (may be fewer than expected due to timeout)
<a name="line124"></a> *     &#39;maximum&#39;: longest execution time (ms)
<a name="line125"></a> *     &#39;minimum&#39;: shortest execution time (ms)
<a name="line126"></a> *     &#39;standardDeviation&#39;: sample standard deviation (ms)
<a name="line127"></a> *     &#39;total&#39;: total execution time (ms)
<a name="line128"></a> *   }
<a name="line129"></a> * &lt;/pre&gt;
<a name="line130"></a> *
<a name="line131"></a> * @param {Function} testFn Test function whose performance is to
<a name="line132"></a> *     be measured.
<a name="line133"></a> * @return {Object} Object containing performance stats.
<a name="line134"></a> */
<a name="line135"></a>goog.testing.PerformanceTimer.prototype.run = function(testFn) {
<a name="line136"></a>  var samples = [];
<a name="line137"></a>  var testStart = goog.now();
<a name="line138"></a>
<a name="line139"></a>  for (var i = 0; i &lt; this.numSamples_; i++) {
<a name="line140"></a>    var sampleStart = goog.now();
<a name="line141"></a>    testFn();
<a name="line142"></a>    var sampleEnd = goog.now();
<a name="line143"></a>    samples[i] = sampleEnd - sampleStart;
<a name="line144"></a>    if (sampleEnd - testStart &gt; this.timeoutInterval_) {
<a name="line145"></a>      // Timed out.
<a name="line146"></a>      break;
<a name="line147"></a>    }
<a name="line148"></a>  }
<a name="line149"></a>
<a name="line150"></a>  if (this.discardOutliers_ &amp;&amp; samples.length &gt; 2) {
<a name="line151"></a>    goog.array.remove(samples, Math.min.apply(null, samples));
<a name="line152"></a>    goog.array.remove(samples, Math.max.apply(null, samples));
<a name="line153"></a>  }
<a name="line154"></a>
<a name="line155"></a>  return {
<a name="line156"></a>    &#39;average&#39;: goog.math.average.apply(null, samples),
<a name="line157"></a>    &#39;count&#39;: i,
<a name="line158"></a>    &#39;maximum&#39;: Math.max.apply(null, samples),
<a name="line159"></a>    &#39;minimum&#39;: Math.min.apply(null, samples),
<a name="line160"></a>    &#39;standardDeviation&#39;: goog.math.standardDeviation.apply(null, samples),
<a name="line161"></a>    &#39;total&#39;: goog.math.sum.apply(null, samples)
<a name="line162"></a>  };
<a name="line163"></a>};
</pre>


</body>
</html>
